﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Get_Article_SKUController : UserController
    {
        public IArticle_goodsService _article_goods { get; set; }
        public IUser_group_priceService _user_group_price { get; set; }
        public IUser_groupsService _user_groups { get; set; }
        public IArticle_goods_specService _article_goods_spec { get; set; }
        public IArticle_specService _article_spec { get; set; }
        [HttpGet]
        public IActionResult Get(int article_id)
        {
            int channel_id = 2;
            if (channel_id == 0 || article_id == 0)
            {
                return Ok(new { status = 0, msg = "传输参数不正确" });
            }
            //查询是否登录，有则查询会员价格
            Users userModel = GetUserInfo();
            //先查有哪些规格组合article_goods_spec
            var goods_spec = _article_goods_spec.GetList(article_id, "");//此产品的所有规格
            List<DTO.Goods.tree> trees = new List<DTO.Goods.tree>();
            var goods_spec0 = goods_spec.Where(a => a.parent_id == 0).ToList();
            int ss = 1;
            foreach (var item in goods_spec0)//规格名称
            {
                DTO.Goods.tree tree = new DTO.Goods.tree();
                tree.k = item.title;
                List<DTO.Goods.vs> vs = new List<DTO.Goods.vs>();
                var goods_spec1 = goods_spec.Where(a => a.parent_id == item.spec_id).ToList();//规格值表
                foreach (var item1 in goods_spec1)
                {
                    DTO.Goods.vs v = new DTO.Goods.vs();
                    v.id = item1.spec_id;
                    v.name = item1.title;
                    vs.Add(v);
                }
                tree.v = vs;
                if(ss==1)
                    tree.k_s = "s1";
                if (ss == 2)
                    tree.k_s = "s2";
                if (ss >= 3)
                    tree.k_s = "s3";
                ss++;
                trees.Add(tree);
            }
            //再查组合值article_goods
            var article_goods = _article_goods.GetList(article_id);//此产品下面的所有规格组合商品
            List<DTO.Goods.zuhe> list = new List<DTO.Goods.zuhe>();
            foreach (var item in article_goods)
            {
                DTO.Goods.zuhe zuhe = new DTO.Goods.zuhe();
                zuhe.id = item.id;
                if (trees.Where(a => a.k_s == "s1").Count() > 0)
                {
                    zuhe.s1 = item.spec_ids.Split(',')[1];
                }
                if (trees.Where(a => a.k_s == "s2").Count() > 0)
                {
                    zuhe.s2 = item.spec_ids.Split(',')[2];
                }
                if (trees.Where(a => a.k_s == "s3").Count() > 0)
                {
                    zuhe.s3 = item.spec_ids.Split(',')[2];
                }
                zuhe.stock_num = item.stock_quantity;
                zuhe.price = item.sell_price;                
                if (userModel != null)
                {
                    //查询会员组价格
                    User_group_price userPriceModel = _user_group_price.GetModel(item.id, userModel.group_id);
                    if (userPriceModel != null)
                    {
                        zuhe.price = userPriceModel.price;
                    }
                    else
                    {
                        //查询会员组折扣
                        User_groups groupModel = _user_groups.GetModel(userModel.group_id);
                        if (groupModel != null)
                        {
                            zuhe.price = item.sell_price * groupModel.discount / 100;
                        }
                    }
                }
                list.Add(zuhe);
            }
            //以JSON格式输出商品信息
            return Ok(new
            {
                status = 1,
                sku = new
                {
                    tree = trees,
                    list = list
                }
            }) ; 
        }
    }
}